from flask import Flask, render_template
import pandas as pd
'''
蚂蚁学python教程
pandas数据web显示
pd.read_csv 读取格式化数据
df_male.to_html 转换为html表格格式render_template到user_info.html（viewport适应小屏幕）
style.css样式 表格只有下框线、tr:nth-child(odd)  tr:nth-child(even) 隔行换色、tr:hover 鼠标在上变色

'''
app = Flask(__name__)


def read_data():
    return pd.read_csv("./db/users.dat",
                       sep="::",
                       engine="python",
                       header=None,
                       encoding="utf-8",
                       names="学号::性别::年龄::职业::邮编".split("::")
                       )


@app.route('/get_user_info')
def get_user_info():
    df = read_data()
    df_male = df[df["性别"] == 'M'].head()
    df_female = df[df["性别"] == 'F'].head()

    return render_template(
        "user_info.html",
        male_data=df_male.to_html(classes="male", index=False),
        female_data=df_female.to_html(classes="female", index=False)
    )


if __name__ == '__main__':
    app.run(
        debug=True
    )
